<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: attachments.php Sat Jul 18 13:17:46 CST 2009
 */

if (false === defined("XOOPS_ROOT_PATH")) {	exit();}

class groupsAttachments extends XoopsObject 
{
	public function __construct() {
		$this->initVar("att_id", XOBJ_DTYPE_INT, null, false); // 附件ID
		$this->initVar("uid", XOBJ_DTYPE_INT); // 上传者
		$this->initVar("tid", XOBJ_DTYPE_INT); // 所属主题ID
		$this->initVar("post_id", XOBJ_DTYPE_INT); // 所属主题ID
		$this->initVar("att_filename", XOBJ_DTYPE_TXTBOX); // 显示名称
		$this->initVar("att_description", XOBJ_DTYPE_TXTAREA); // 描述
		$this->initVar("att_attachments", XOBJ_DTYPE_TXTBOX); // 文件名称
		$this->initVar("att_type", XOBJ_DTYPE_TXTBOX); // 文件属性
		$this->initVar("att_size", XOBJ_DTYPE_INT); // 文件大小
		$this->initVar("att_thumb", XOBJ_DTYPE_INT); // 是否有缩图
		$this->initVar("att_dateline", XOBJ_DTYPE_INT); // 上传时间
		$this->initVar("att_downloads", XOBJ_DTYPE_INT); // 下载次数
	}
	
	public function Icon(){
	    $img = array("image/bmp","image/gif","image/jpeg","image/png","image/tiff","image/tif");
        if ( in_array($this->getVar("att_type"),$img) ) {
            return "<img src=\"images/icon_img.gif\" alt=\"\" />";
        } else {
            return "<img src=\"images/zip.gif\" alt=\"\" />";
        }
        return false;
	}
	
	public function fileSize(){
       return number_format($this->getVar("att_size")/1024,2) . "Kb";
    }
}

class groupsAttachmentsHandler extends XoopsPersistableObjectHandler
{
	public function __construct(&$db) {
        parent::__construct($db,"groups_attachments","groupsAttachments","att_id");
    }
    
    public function setAttachments($tid,$post_id,$uid){
    	global $xoopsModuleConfig;
    	include_once (dirname(__FILE__)."../../include/functions.uploader.php");
    	$num = 0;
    	for ($i=0; $i<$xoopsModuleConfig["attnum"]; $i++ ) {
    		if ( !$_FILES[$_POST["xoops_upload_file"][$i]]["size"] ) continue;
	    	$uploadFiledir = filesUploader::mkUploadsDir();
			$uploadfile_obj = filesUploader::uploadFile($uploadFiledir["path"],$i);
			if ( false === $uploadfile_obj )  continue;
			$att_obj = $this->get();
			$att_obj->setVar("uid",$uid);
			$att_obj->setVar("tid",$tid);
			$att_obj->setVar("post_id",$post_id);
			$att_obj->setVar("att_filename",$uploadfile_obj->getMediaName());
			$att_obj->setVar("att_attachments",$uploadFiledir["dirname"]."/".$uploadfile_obj->getSavedFileName());
			$att_obj->setVar("att_type",$uploadfile_obj->getMediaType());
			$att_obj->setVar("att_size",$uploadfile_obj->getMediaSize());
			$att_obj->setVar("att_dateline",time());
			if ( $att_id = $this->insert($att_obj) ) {
			    $num++;
			}
    	}
		return $num;
    }
    
    public function getAllAttachmentList($post_id) {
        $criteria = new CriteriaCompo(new Criteria("post_id",$post_id));
        $att_objs = $this->getAll($criteria);
        $list = array();
        if ( $att_objs ) {
            foreach ( $att_objs as $k=>$obj ) {
                $list[$k]  = $obj->getValues();
                $list[$k]["icon"]  = $obj->Icon();
            }
        }
        return $list;
    }
    
}
